package com.gxgeek.springfeaturesstudy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.event.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.RequestHandledEvent;

/**
 * @author gaoxing
 * @create 2017-11-07 下午12:00
 */
@Component
@Slf4j
public class EventListenerService {



    @EventListener({ContextStartedEvent.class})//无任何调用
    public void handleContextStartedEvent(ContextStartedEvent event) {
        event.getApplicationContext();

        log.info(getClass().getSimpleName()+"：ContextStartedEvent 事件");
    }
    @EventListener(classes = ContextRefreshedEvent.class)
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        log.info(getClass().getSimpleName()+"：ContextRefreshedEvent 事件，当ApplicationContext容器初始化完成或者被刷新的时候，就会发布该事件。");
    }

    @EventListener({ContextStoppedEvent.class})//无任何调用
    public void handleContextStoppedEvent(ContextStoppedEvent event) {

        log.info(getClass().getSimpleName()+"：ContextStoppedEvent 事件");
    }
    @EventListener({ContextClosedEvent.class})
    public void handleContextClosedEvent(ContextClosedEvent event) {

        log.info(getClass().getSimpleName()+"：ContextClosedEvent 事件  调用ConfigurableApplicationContext的close方法的时候，关闭指的是所有的单例Bean都被销毁。");
    }

    @EventListener({RequestHandledEvent.class})
    public void handleRequestHandledEvent(RequestHandledEvent event) {

        log.info(getClass().getSimpleName()+"：RequestHandledEvent 事件 处理用户请求结束后，系统会触发该事件。");
    }

    @EventListener({ApplicationReadyEvent.class})
    public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
        event.getApplicationContext();

        log.info(getClass().getSimpleName()+"：ApplicationReadyEvent 事件 ----> spring boot 启动后调用 该事件表示application应该初始化完成，可以准备接收请求。\n");
    }
    @EventListener({ApplicationStartingEvent.class})//无任何调用
    public void handleApplicationStartingEvent(ApplicationStartingEvent event) {

        log.info(getClass().getSimpleName()+":ApplicationStartingEvent 事件");
    }
    @EventListener({ApplicationPreparedEvent.class})//无任何调用,需要使用add
    public void handleApplicationPreparedEvent(ApplicationPreparedEvent event) {

        log.info(getClass().getSimpleName()+":ApplicationPreparedEvent 事件");
    }


}
